package com.wzh.house.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.Gson;
import com.wzh.house.entity.UserLogEntity;
import com.wzh.house.service.UserLogService;
import com.wzh.house.vo.SysUserVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Date;

@Aspect
@Component
public class newHouseAopAspect {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private UserLogService userLogService;
    /**
     * 定义一个切入点表达式,用来确定哪些类需要代理
     * execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理
     */
    @Pointcut("execution(* com.wzh.house.controller.NewHouseController.save(..))")
    public void save() {}
    @Pointcut("execution(* com.wzh.house.controller.NewHouseController.update(..))")
    public void update() {}
    @Pointcut("execution(* com.wzh.house.controller.NewHouseController.delete(..))")
    public void delete() {}
    /**
     * 后置方法,在目标方法执行后执行
     * @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写
     */
    @After("save()")
    public void saveMethod(JoinPoint joinPoint){
        System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
        System.out.println("目标方法所属类的简单类名:" +joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        //获取传入目标方法的参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第" + (i+1) + "个参数为:" + args[i]);
        }
        System.out.println("被代理的对象:" + joinPoint.getTarget());
        System.out.println("代理对象自己:" + joinPoint.getThis());
        UserLogEntity userLogEntity = new UserLogEntity();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        userLogEntity.setMethod(className + "." + methodName + "()");

        //请求的参数
        try{
            String params = new Gson().toJson(args);
            userLogEntity.setParams(params);
        }catch (Exception e){
        }
        //用户名 redis中取
        String user = redisTemplate.opsForValue().get("user");
        SysUserVo SysUserVo = JSON.parseObject(user, new TypeReference<SysUserVo>() {
        });
        userLogEntity.setUsername(SysUserVo.getUsername());

        userLogEntity.setCreateDate(new Date());
        userLogEntity.setOperation(joinPoint.getSignature().getName()+"新房、二手房添加操作");
        userLogService.save(userLogEntity);

    }
    @After("update()")
    public void updateMethod(JoinPoint joinPoint){
        System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
        System.out.println("目标方法所属类的简单类名:" +joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        //获取传入目标方法的参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第" + (i+1) + "个参数为:" + args[i]);
        }
        System.out.println("被代理的对象:" + joinPoint.getTarget());
        System.out.println("代理对象自己:" + joinPoint.getThis());
        UserLogEntity userLogEntity = new UserLogEntity();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        userLogEntity.setMethod(className + "." + methodName + "()");

        //请求的参数
        try{
            String params = new Gson().toJson(args);
            userLogEntity.setParams(params);
        }catch (Exception e){

        }
        //用户名 redis中取
        String user = redisTemplate.opsForValue().get("user");
        SysUserVo SysUserVo = JSON.parseObject(user, new TypeReference<SysUserVo>() {
        });
        userLogEntity.setUsername(SysUserVo.getUsername());

        userLogEntity.setCreateDate(new Date());
        userLogEntity.setOperation(joinPoint.getSignature().getName()+"新房、二手房修改操作");
        userLogService.save(userLogEntity);

    }
    @After("delete()")
    public void deleteMethod(JoinPoint joinPoint){
        System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
        System.out.println("目标方法所属类的简单类名:" +joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        //获取传入目标方法的参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第" + (i+1) + "个参数为:" + args[i]);
        }
        System.out.println("被代理的对象:" + joinPoint.getTarget());
        System.out.println("代理对象自己:" + joinPoint.getThis());
        UserLogEntity userLogEntity = new UserLogEntity();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        userLogEntity.setMethod(className + "." + methodName + "()");

        //请求的参数
        try{
            String params = new Gson().toJson(args);
            userLogEntity.setParams(params);
        }catch (Exception e){

        }
        //用户名 redis中取
        String user = redisTemplate.opsForValue().get("user");
        SysUserVo SysUserVo = JSON.parseObject(user, new TypeReference<SysUserVo>() {
        });
        userLogEntity.setUsername(SysUserVo.getUsername());

        userLogEntity.setCreateDate(new Date());
        userLogEntity.setOperation(joinPoint.getSignature().getName()+"新房、二手房删除操作");
        userLogService.save(userLogEntity);

    }
}
